/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package cc.aileron.commons.logging;

import static cc.aileron.commons.utility.Quote.quote;
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * LoggingInterface のdefault実装
 * @author Aileron
 *
 */
public class LoggingInterfaceDefault implements LoggingInterface
{
    /**
     * ログ表示時間フォーマット
     */
    private final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    
    
    public void startLog(LoggingLevel level, String targetClass,
            String method, String message)
    {
        System.out.println( "<log" 
                + " time="  + quote(format.format(new Date()))
        		+ " level=" + quote(level.name()) 
                + " class=" + quote(targetClass) 
                + " method="+ quote(method) +">" );
    }

    
    public void invokeLog(List<Object> value)
    {
        System.out.println("<args>");
        for (Object object : value)
        {
            if(object==null)
            {
                System.out.println( "<arg />" );
                continue;
            }
            if(object.getClass().isArray())
            {
                final Object[] array = (Object[]) object;
                if(array.length==1)
                {
                    System.out.println( "<arg>" + quoteXml(array[0]) + "</arg>" );
                    continue;
                }
                
                System.out.println( "<array>" );
                for(final Object element: array)
                {
                    System.out.println( "<element>" + quoteXml(element) + "</element>" );
                }
                System.out.println( "</array>" );
                continue;
            }
            System.out.println( "<arg>" + quoteXml(object) + "</arg>" );
        }
        System.out.println("</args>");
        System.out.println("<invoke>");
    }
    
    public void resultLog(Object value)
    {
        System.out.println( "</invoke>" );
        System.out.println( "<result>" + quoteXml(value) + "</result>" );
        System.out.println( "</log>" );
    }
    
    private String quoteXml(Object object)
    {
        if(object==null)
        {
            return "";
        }
        return escapeHtml(object.toString());
    }
}